﻿using System.Data;
using Database.Context;
using QuanLyThongTinCoSo.BaseForm;
using QuanLyThongTinCoSo.Biz;
using QuanLyThongTinCoSo.Common;
using Model = Database.Models;

namespace QuanLyThongTinCoSo.Forms.PhieuDieuTraCoSo
{
    public partial class frmKiemTraAnToan : frmDetail
    {
        public frmKiemTraAnToan()
        {
            InitializeComponent();
            Text = @"Kiểm tra an toàn PCCC";
            dtNgay.Focus();
        }

        public frmKiemTraAnToan(int id)
        {
            InitializeComponent();
            Text = @"Sửa Thẩm duyệt PCCC";

            dtNgay.Focus();
            _fromtype = Constant.TypeForm.Edit;
            _id = id;

            var biz = new KiemTraAnToanBiz();
            var model = biz.GetItemById(id) as Model.KiemTraAnToan;
            if (model == null) return;

            drlCheDoKiemTra.SelectedValue = model.CheDoKiemTra;
            dtNgay.Value = model.Ngay;
            txtTonTai.Text = model.TonTai;
            txtKienNghi.Text = model.KienNghi;
        }

        private void LoadCheDoKiemTra()
        {
            var dt = new DataTable();
            dt.Columns.Add("Id");
            dt.Columns.Add("Value");
            dt.Rows.Add("Định kỳ", "Định kỳ");
            dt.Rows.Add("Đột xuất", "Đột xuất");
            drlCheDoKiemTra.DataSource = dt;
            drlCheDoKiemTra.DisplayMember = "Value";
            drlCheDoKiemTra.ValueMember = "Id";
        }

        private void frmKiemTraAnToan_Load(object sender, System.EventArgs e)
        {
            LoadCheDoKiemTra();
        }

        #region Validation

        protected override bool Validation()
        {
            bool flag = true;

            errorProvider.Dispose();

            if (string.IsNullOrEmpty(txtTonTai.Text))
            {
                errorProvider.SetError(txtTonTai, "Bạn chưa nhập Tồn tại, thiếu sót về an toàn PCCC. Xin vui lòng kiểm tra lại");
                flag = false;
            }

            return flag;
        }

        #endregion

        #region Insert

        protected override void Insert()
        {
            var context = new Context();
            var model = new Model.KiemTraAnToan
            {
                Ngay = dtNgay.Value,
                CheDoKiemTra = (string) drlCheDoKiemTra.SelectedValue,
                TonTai = txtTonTai.Text.Trim(),
                KienNghi = txtKienNghi.Text.Trim()
            };
            var biz = new KiemTraAnToanBiz(context);
            biz.Insert(model);
        }

        #endregion

        #region Edit

        protected override void Edit()
        {
            var context = new Context();
            var biz = new KiemTraAnToanBiz(context);
            var item = biz.GetItemById(_id) as Model.KiemTraAnToan;
            if (item != null)
            {
                item.Ngay = dtNgay.Value;
                item.CheDoKiemTra = (string) drlCheDoKiemTra.SelectedValue;
                item.TonTai = txtTonTai.Text.Trim();
                item.KienNghi = txtKienNghi.Text.Trim();
                biz.Update(item);
            }
        }

        #endregion
}
}